<template>
  <view class="n-home-page bg-bgf6" :style="{ paddingTop: h + 44 + 'px' }">
    <view class="myfix">
      <wTitleBar
        :bgColor="addBackground"
        :isReturn="true"
        :goHome="false"
        :isMain="false"
        :returnColor="1"
        title="创建WIFI"
        fixed="none"></wTitleBar>
    </view>
    <view class="wifi-view-top">
      <view class="wview">
        <img src="../static/img/ic_add_wifi.png" />
        <text>一键快速链接</text>
      </view>
      <view class="wview">
        <img src="../static/img/ic_add_clock.png" />
        <text>无需告知密码</text>
      </view>
      <view class="wview">
        <img src="../static/img/ic_add_save.png" />
        <text>安全防蹭网</text>
      </view>
    </view>
    <view class="add-view">
      <view class="container-user-body-items">
        <text>商户名称</text>
        <input type="text" placeholder="请输入商户名称" placeholder-style="color:#C0C4CC;" :maxlength="20"
               v-model="form.wifiStoreName"
        />
      </view>

      <view class="container-user-body-items">
        <text class="input-name">WiFi名称</text>
        <input type="text" placeholder="请输入WiFi名称" placeholder-style="color:#C0C4CC;" :maxlength="20"
               v-model="form.wifiSsid"
        />
      </view>
      <view class="container-user-body-items">
        <text>WiFi密码</text>
        <input type="text" placeholder="请输入WiFi密码" placeholder-style="color:#C0C4CC;" :maxlength="20"
               v-model="form.wifiPassword"
        />
      </view>

      <view style="display: flex;align-items:center;margin-left: 10px">

        <img style="width: 12px;height: 12px" src="../static/img/ic_add_tip.png" />

        <text style="font-size: 10px;color: #07c160;margin-left: 5px">WiFi信息加密上传且不会被共享，请放心填写</text>


      </view>
    </view>
    <view class="submit-btn" @tap="submit">
      <text>一键生成WiFi码</text>
    </view>
    <view style="position: center; width: 100%; height: 100vw" v-if="canvasShow">
      <share-canvas :imgUrl="imagePath" @close="canvasShow = false"></share-canvas>
    </view>
    <public-modal v-model="showPublicModal">
      <canvas canvas-id="mycanvas" style="margin: 20px auto; width: 230px; height: 230px" />
    </public-modal>
  </view>
</template>

<script>
import wTitleBar from "@/components/w-titleBar/w-titleBar";
import mCommon from "@/api/common";
import drawQrcode from "@/utils/qrcode";
import ShareCanvas from "@/pages-wifi/shareCodeCanvas.vue";
import publicModal from "@/pages-wifi/publicModal";
import QRCode from "@/utils/qrcode";

export default {
  components: {
    wTitleBar, ShareCanvas, publicModal
  },
  data() {
    return {
      h: uni.getSystemInfoSync().statusBarHeight, // 手机状态栏的高度
      addBackground: "#366bf9",
      showPublicModal: false,
      show: false,
      canvasStyle: "width: 750px; height: 1300px",
      canvasShow: false,
      form: {
        wifiSsid: "",
        wifiPassword: "",
        wifiStoreName: ""
      },
      typeName: "",
      maskHidden: true,
      defaultUrl: "https://ddsssy.com/prod-api/profile/upload/",
      imagePath: ""//wxapp-union.com" //默认二维码生成文本

    };
  },
  mounted() {

  },


  methods: {

    submit() {
      const vm = this;
      if (!this.form.wifiSsid) {
        uni.showToast({
          title: "请填写wifi名称",
          icon: "none"
        });
        return false;
      }
      if (!this.form.wifiPassword) {
        uni.showToast({
          title: "请填写wifi密码",
          icon: "none"
        });
        return false;
      }
      mCommon.searchWifiData({ wifiSsid: this.form.wifiSsid }).then((res) => {
        console.log(res, "返回数据>>>>>>>>>>>>");
        if (res.total === 1) {
          uni.showToast({
            title: "wifi名称已存在",
            icon: "none"
          });
        } else {
          mCommon.addWifiData(this.form).then((res) => {
            if (res.code === 200) {
              uni.showToast({
                title: "提交成功",
                icon: "none"
              });
              this.showPublicModal = true;
              setTimeout(function() {
                this.canvasShow = true;
                // 页面初始化 options为页面跳转所带来的参数
                const qrcode = new QRCode("mycanvas", {
                  width: 230,
                  height: 230,
                  colorDark: "#000000",
                  colorLight: "white",
                  correctLevel: QRCode.CorrectLevel.H
                });


                qrcode.makeCode(`${vm.defaultUrl}?wifissid=${vm.form.wifiSsid}&wifipassword=${vm.form.wifiPassword}`);
                /*     wx.showActionSheet({
                      itemList: ["保存图片"],
                      success: function(res) {
                        console.log(res.tapIndex);
                        if (res.tapIndex == 0) {
                          qrcode.exportImage(function(path) {
                            wx.saveImageToPhotosAlbum({
                              filePath: path
                            });
                          });
                        }
                      }
                    }); */
              }, 500);
            } else {
              uni.showToast({
                title: res.msg,
                icon: "none"
              });
            }
          });
        }
      });

    }
  }

};
</script>

<style lang="scss" scoped>

.n-home-page {
  width: 100%;

  .wifi-view-top {
    display: flex;
    margin-left: 10px;
    margin-right: 10px;
    margin-top: 20px;
    justify-content: space-between;

    .wview {
      display: flex;

      img {
        height: 14px;
        width: 14px;
      }

      text {
        font-size: 12px;
        margin-left: 5px;
        color: #3c3c3c;
      }

    }

  }

  .add-view {
    margin: 20px 15px 0;
    background-color: #fff;
    border-radius: 8px;
    overflow-x: hidden;
    padding: 0 10px 10px;
    height: auto;
    flex-direction: column;
    display: flex;

    .otx {
      color: #3e85ff;
      font-weight: bold;
      margin-bottom: 5px;
      font-size: 14px;
    }

    .container-user-body-items {
      height: 44px;
      display: flex;
      align-items: center;
      margin: 10px 10px;
      border-bottom: 1px solid #f3f3f3;
    }

    input {
      margin-left: 10px;
    }

    text {
      color: #303133;

    }


  }

  .submit-btn {
    display: flex;
    justify-content: center;
    margin: 20px 0;

    text {
      width: 90%;
      background: #3e85ff;
      border-radius: 4px;
      height: 34px;
      justify-content: center;
      display: flex;
      align-items: center;
      color: #fff;
      font-size: 14px;
    }
  }

  .container-box {
    background-color: #07c160;
    height: 100vh
  }

  .img-box {
    padding: 32 rpx;
    background-color: #fff;
    border-bottom: 1 rpx solid #e5e5e5;
    position: relative
  }

  .img-box image {
    width: 686 rpx;
    height: 686 rpx;
    background-color: #f9f9f9
  }

  .input-row {
    margin: 30 rpx auto;
    border-bottom: 1 rpx solid #e5e5e5;
    border-top: 1 rpx solid #e5e5e5;
    display: flex;
    align-items: center;
    height: 88 rpx;
    padding: 0 32 rpx;
    background-color: #fff;
    font-size: 34 rpx;
    color: #000
  }

  .input-row input {
    margin-left: 100 rpx;
    flex: 1
  }

  .mybtn {
    width: 686 rpx;
    margin: 60 rpx auto
  }

  .bg {
    position: fixed;
    top: 0;
    background: rgba(0, 0, 0, 0.5);
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2001;
    flex-direction: column;
  }

  .pc-container {
    width: 640 rpx;
    height: 640 rpx;
    margin: 0 auto;
    overflow: hidden;
  }

  .pc-container-image {
    width: 640 rpx;
    height: 640 rpx;
    display: block;
  }

  .canvas {
    position: absolute;
    right: -2000rpx;
  }

  .share-card {
    position: absolute;
    top: 0;
    left: 0;
  }

  .canvas-box {
    position: fixed;
    top: 999999 rpx;
    left: 0
  }
}

.order {
  text-align: center;
  padding: 40px 0 36px 0;
  color: #333;
  font-size: 16px;

  .red-col {
    color: #ff3556;
  }

  .order-btn {
    width: 110px;
    height: 32px;
    line-height: 32px;
    border-radius: 100px;
    background: #ff3556;
    color: #fff;
    font-size: 18px;
    border: 0;
    margin: 0 auto;
  }
}

.closeBtn {
  width: 54 rpx;
  height: 54 rpx;
  line-height: 54 rpx;
  text-align: center;
  border: 1px solid #fff;
  border-radius: 100px;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 6%;
  margin: auto;
  color: #fff;
  z-index: 100;
  font-size: 30 rpx;
}


</style>
